<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="utf-8">
var admin_hj_datagrid;
var admin_hj_editRow = undefined;
var admin_hj_addRow = undefined;
var admin_hj_searchForm = undefined;
$(function(){
		
		admin_hj_searchForm = $('#admin_hj_searchForm').form();
	  $("#admin_hj_searchButton").bind('click', function(){   
		  
		  admin_hj_search(); 
	    }); 
	  $("#admin_hj_CleanSearchButton").bind('click', function(){   
		  
		  admin_hj_cleanSearch();
    }); 
		
	  admin_hj_datagrid=$('#admin_hj_datagrid').datagrid({
		url : '${pageContext.request.contextPath}/hjAction!dataGrid.action',
		title:'货架信息查询',
		iconCls:'icon-search',
		toolbar : '#admin_hj_toolbar',
		fit : true,
		border : false,
		//animate : true,
		//singleSelect : false,	//false是打开全选功能
		striped:true,
		idField : 'id',
		rownumbers: true,		//显示行号
		fitColumns : true,		//显示横向滚动条
		pagination : true,
		pageSize : 20,
		pageList : [ 10, 20, 30, 40, 50 ],
		sortName:'cjsj',
		sortOrder:'desc',
		frozenColumns:[[
				{
					title:'编号',
					field:'id',
					width:100,
					checkbox : true,
				},
				{
					title:'货架编号',
					field:'numberID',
					width:100,
					sortable:'true',
					editor:{
						type:'validatebox',
						options:{
							required:true, 
							missingMessage:'库房编号必填'
						}
					}
				}
		]],	
		columns : [[{
					title : '库房',
					field : 'kfList',
					width : 80,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value){
							return value[0];
						}
					},
					editor:{
						type:'comboboxFk',
						options:{
							required:true,
							valueField:'id',
							textField:'name',
							url:'${pageContext.request.contextPath}/kfAction!comboboxInfo.action',
							editable:false,
							missingMessage:'库房必填'
							
						}
					}
			
				},{
					title:'货架名称',
					field:'name',
					width:100,
					sortable:'true',
					editor:{
						type:'text',
					}
				},{
					title : '备注',
					field : 'bz',
					width : 130,
					align : 'center',
					sortable:true,
					editor:{
						type:'text'
					}
			
				},{
					title : '状态',
					field : 'flag',
					width : 80,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value == "T"){
							return "开启";
						}else{
							return "关闭";
						}
					},
					editor:{
						type:'combobox',
						options:{
							required:true,
							valueField:'flag',
							textField:'txtValue',
							data: [{
									txtValue: '开启',
									flag: 'T'
								},{
									txtValue: '关闭',
									flag: 'F'
								}],
							editable:false,
							missingMessage:'状态必填'
							
						}
					}
			
				},{
					title:'创建时间',
					field:'cjsj',
					width:100,
					sortable:true,
					editor:{
						type:'datetimebox',
						options:{
							editable:false,
							required:true
						}
					}
				}]],
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_hj_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_hj_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_hj_editRow = undefined;
					admin_hj_addRow = undefined;
					admin_hj_datagrid.datagrid('unselectAll');
					return;
				}
				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/hjAction!add.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/hjAction!edit.action';
				}
				$.ajax({
					url : url,
					data : rowData,
					dataType : 'json',
					type:'POST',
					success : function(r) {
						if (r.success) {
							admin_hj_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'
							});
							admin_hj_editRow = undefined;
							admin_hj_addRow = undefined;
							admin_hj_datagrid.datagrid('load');
						} else {
							admin_hj_datagrid.datagrid('rejectChanges');
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_hj_datagrid.datagrid('unselectAll');
						admin_hj_editRow = undefined;
						admin_hj_addRow = undefined;
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#admin_hj_easyui-menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			},
			onDblClickRow : function(rowIndex, rowData) {
				if(admin_hj_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					
					if (admin_hj_editRow != undefined) {
						admin_hj_datagrid.datagrid('endEdit', admin_hj_editRow);
					}

					if (admin_hj_editRow == undefined) {
						admin_hj_datagrid.datagrid('beginEdit', rowIndex);
						admin_hj_editRow = rowIndex;
						admin_hj_datagrid.datagrid('unselectAll');
					}
				}
				
			}
		
		
			});
			
		});
		function admin_hj_appendColumns(){
			
			if (admin_hj_editRow != undefined||admin_hj_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
		
			else{
				admin_hj_datagrid.datagrid('unselectAll');
		
		
				var row = {};
				admin_hj_datagrid.datagrid('insertRow', {
					index : 0,
					row : row
				});
				admin_hj_addRow = 0;
				admin_hj_datagrid.datagrid('selectRow', admin_hj_addRow);
				admin_hj_datagrid.datagrid('beginEdit', admin_hj_addRow);
			}
		}

		function admin_hj_edit() {
			var rows = admin_hj_datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				if(admin_hj_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_hj_editRow != undefined) {
						admin_hj_datagrid.datagrid('endEdit', admin_hj_editRow);
					}
					if (admin_hj_editRow == undefined) {
						admin_hj_editRow = admin_hj_datagrid.datagrid('getRowIndex', rows[0]);
						admin_hj_datagrid.datagrid('beginEdit', admin_hj_editRow);
						admin_hj_datagrid.datagrid('unselectAll');
					}
				}
				
			} else {
				$.messager.show({
					msg : '请选择一项进行修改！',
					title : '错误'
				});
			}
		}
		function admin_hj_search() {
			if(admin_hj_editRow != undefined||admin_hj_addRow!=undefined){
				$.messager.show({
					msg : '请先保存或撤销编辑！',
					title : '提示'
				});
			}else{
				admin_hj_datagrid.datagrid('load', serializeObject(admin_hj_searchForm));
			}
			
		}
		function admin_hj_cleanSearch() {
			admin_hj_editRow = undefined;
			admin_hj_addRow=undefined;
			admin_hj_datagrid.datagrid('load', {});
			admin_hj_searchForm.find('input').val('');
		}
		function admin_hj_del() {
			if (admin_hj_editRow != undefined||admin_hj_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
			var rows = admin_hj_datagrid.datagrid('getSelections');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/hjAction!delete.action',
							data : {
								ids : ids.join(',')
							},
							type:'POST',
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									admin_hj_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_hj_editRow = undefined;
									admin_hj_addRow = undefined;
									admin_hj_datagrid.datagrid('clearSelections');
									admin_hj_datagrid.datagrid('load');
								} else {
									admin_hj_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'error');
			}
		}
		
		function admin_hj_export_excel() {
			var url1 = '${pageContext.request.contextPath}/hjAction!exportHjExcel.action?kfID='+$('#kfID').combobox("getValue");
			window.open (url1,'newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');			
		}

		
		/*文件发送后台操作*/
		var admin_hj_import_dialog;
		function admin_hj_import_excel() {
			 $('#admin_hj_import_dialog').show();
				admin_hj_import_dialog = $('#admin_hj_import_dialog').dialog({
					collapsible: false,
					minimizable: false,
					maximizable: false,
					iconCls:'icon-undo',
					modal:true,//开启幕布
					buttons:[{
							text: '确认提交',
							iconCls: 'icon-ok',
							handler: function() { 
								 $('#admin_hj_importForm').submit(); 
							}}
							]
			});
		}
		function close(){
			admin_hj_import_dialog.dialog('close');
		}

		$('#admin_hj_importForm').form({
			 url:'${pageContext.request.contextPath}/hjAction!upHjFile.action',
		     dataType:'json',
		     success:function(j){
		     	//admin_hj_datagrid.datagrid('acceptChanges');
		       close();
		       j = $.parseJSON(j); 
					if(j&&j.success){
						$.messager.show({
							msg:j.msg,
							title:'提示'
						});
						 admin_hj_editRow = undefined;
						admin_hj_addRow = undefined;
						admin_hj_datagrid.datagrid('load');
						$('#hj_upload_excel').val('');
					}else{
							admin_hj_datagrid.datagrid('rejectChanges');
							$.messager.alert('提示',j.msg,'warning');
					}
				},
		     onSubmit:function(){
				if ($('#hj_upload_excel').val() == "") {
					alert("请点击浏览按钮，选择您要上传的文件!");
					return (false);
				} else {
					var str = $('#hj_upload_excel').val();
					//console.info($('#upload').val());
					var strs = str.toLowerCase();
					var lens = strs.length;
					var extname = strs.substring(lens - 4, lens);
					if (extname == ".xls" || extname == "xlsx") {
						return $(this).form('validate');		
					}else{
						alert("请选择Execl文件！");
						return (false);
					}
				}
		        
		     }
 	});
 	function admin_hj_save(){
 		if (admin_hj_editRow != undefined) {
			admin_hj_datagrid.datagrid('endEdit', admin_hj_editRow);
			admin_hj_editRow = undefined;
		}
		if (admin_hj_addRow != undefined) {
				admin_hj_datagrid.datagrid('endEdit', admin_hj_addRow);
				admin_hj_datagrid.datagrid('getChanges', 'inserted');
			
		}
 	}
 	function admin_hj_unedit(){
 		admin_hj_datagrid.datagrid('unselectAll');
		admin_hj_datagrid.datagrid('rejectChanges');
		admin_hj_editRow = undefined;
		admin_hj_addRow = undefined;
 	}
</script>
<div id="admin_hj_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div region="center" border="false" style="overflow: hidden;">
		<div id="admin_hj_toolbar" class="datagrid-toolbar" style="height: auto;display: none;">
			<fieldset>
				<legend>筛选</legend>
					<form id="admin_hj_searchForm">
						<table class="admin_hj_searchForm datagrid-toolbar" style="width: 100%;height: 100%;">
						<tr>
						<td>
						<label>库房:&nbsp</label><input name="kfID" id="kfID" class='easyui-combobox' style="width:150px;" data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/kfAction!comboboxInfo.action',editable:false" />
						<a href='javascript:void(0);' id="admin_hj_searchButton" class='easyui-linkbutton' data-options="iconCls:'icon-search'">查询</a>
						<a href='javascript:void(0);' id="admin_hj_CleanSearchButton" class='easyui-linkbutton' data-options="iconCls:'icon-cancel'">清空</a>
						</td>
						</tr>
						</table>
					</form>
			</fieldset>
			<div>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="admin_hj_appendColumns();" plain="true" href="javascript:void(0);">增加</a> 
				<a class="easyui-linkbutton" iconCls="icon-remove" onclick="admin_hj_del();" plain="true" href="javascript:void(0);">删除</a> 
				<a class="easyui-linkbutton" iconCls="icon-edit" onclick="admin_hj_edit();" plain="true" href="javascript:void(0);">编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-save" onclick="admin_hj_save();" plain="true" href="javascript:void(0);">保存</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_hj_unedit();" plain="true" href="javascript:void(0);">取消编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_hj_datagrid.datagrid('unselectAll');" plain="true" href="javascript:void(0);">取消选中</a>
				<a class="easyui-linkbutton" iconCls="icon-xz" onclick="admin_hj_export_excel();" plain="true" href="javascript:void(0);">Excel导出</a>
				<a class="easyui-linkbutton" iconCls="icon-xz" onclick="admin_hj_import_excel();" plain="true" href="javascript:void(0);">Excel导入</a>
			</div>
		</div>
		<table id="admin_hj_datagrid"></table>
	</div>
</div>


<div id="admin_hj_import_dialog"  style="width:250px; height: 200px; display:none;" title="Excel文件上传">
    <form id="admin_hj_importForm" method="post" enctype="multipart/form-data">
		<div>上传文件:<input name="hj_upload_excel" id="hj_upload_excel" type="file"/></div>
	</form>
</div> 